//
//  ViewController.m
//  工厂模式
//
//  Created by Song on 2018/5/18.
//  Copyright © 2018年 Song. All rights reserved.
//

#import "ViewController.h"

#import "PersonFactory.h"
#import "Person.h"
#import "Teacher.h"
#import "Student.h"

#import "Model_A.h"
#import "Model_B.h"

#import "Model_notify.h"
#import "GSY_Notification.h"

#import "SubscriptionServiceCenter.h"
#import "CustomerA.h"

static NSString *SCIENCE = @"SCIENCE";

@interface ViewController () <NSCopying,SubscriptionServiceCenterProtocol>

/** Customer */
@property (nonatomic,strong)CustomerA *a;

@end

//static id gsy;

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // kvo的使用
    Model_A *a = [[Model_A alloc] init];
//    Model_A *aa = [[Model_A alloc] init];
    Model_B *b = [[Model_B alloc] init];
    
    [a addObserver:b forKeyPath:@"name" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
//    [aa addObserver:b forKeyPath:@"name" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        NSLog(@"================================================================");
        NSLog(@"a.name = %@",a.name);
        a.name = @"hello"; // 更改了 a 的name属性， b 会监听，所以跳到 b 的observeValueForKeyPath。
        NSLog(@"a.name = %@",a.name);
        
        NSLog(@"b.name = %@",b.nameB);
        
        [a removeObserver:b forKeyPath:@"name"];
//        [aa removeObserver:b forKeyPath:@"name"];
    });
    
    NSLog(@"================================================================");
    
    // 通知的使用
    Model_notify *mn1 = [[Model_notify alloc] init];
    
    // 增加观察者 mn1，当通知中心发消息，就会触发a对象中的 func 方法
    [GSY_Notification addGSYObserver:mn1 selector:@selector(func:) flag:@"A"];
    
    // 通知中心通过 flag 标志，找到对应的对象，然后给他发消息
    [GSY_Notification sendGSYMessage:@"Hello-a" toFlag:@"A"];
    
    // 移除观察者
    [GSY_Notification removeGSYObserver:mn1 flag:@"A"];
    
    
    Model_notify *mn2 = [[Model_notify alloc] init];
    [GSY_Notification addGSYObserver:mn2 selector:@selector(func:) flag:@"B"];
    [GSY_Notification sendGSYMessage:@"Hello-b" toFlag:@"B"];
    [GSY_Notification removeGSYObserver:mn2 flag:@"B"];
    
    NSLog(@"================================================================");
    
    // 观察者模式 - 订阅书刊
    
    // 创建订阅号
    [SubscriptionServiceCenter createSubscriptionNumber:SCIENCE];
    // 创建订阅号
    [SubscriptionServiceCenter createSubscriptionNumber:@"gsy"];
    
    // 添加第一个订阅的用户到指定的刊物，这里的用户就是当前self：ViewController对象
    [SubscriptionServiceCenter addCustomer:self withSubscriptionNumber:SCIENCE];
    
    // 添加第二个订阅的用户到指定的刊物，这里的用户就是a：CustomerA对象
    self.a = [CustomerA new];
    [SubscriptionServiceCenter addCustomer:self.a withSubscriptionNumber:@"gsy"];
    
    // 发行机构发送消息
    [SubscriptionServiceCenter sendMessage:@"message=V1.0=gsy" toSubscriptionNumber:@"gsy"];
    [SubscriptionServiceCenter sendMessage:@"message=V1.0=viewController" toSubscriptionNumber:SCIENCE];
}

// 实现协议约束的方法
-(void)subscriptionMessage:(id)message subscriptionNumber:(NSString *)subscriptionNumber {
    NSLog(@"%@  %@", message, subscriptionNumber);
}

// 工厂模式
-(void)factoryMethod {
    PersonFactory *personFactory = [[PersonFactory alloc] init];
    
    [personFactory creatObject:TeacherClassName complete:^(Person *object) {
        [object printClassName];
    }];
    
    [personFactory creatObject:StudentClassName complete:^(Person *object) {
        [object printClassName];
    }];
}

//// 单例模式
//+(instancetype)allocWithZone:(struct _NSZone *)zone {
//    if (gsy == nil) {
//        @synchronized (self) {
//            if (gsy == nil) {
//                gsy = [super allocWithZone:zone];
//            }
//        }
//    }
//    return gsy;
//}
//
//-(id)copyWithZone:(NSZone *)zone {
//    return gsy;
//}


@end
